package org.tgit.gateway.config;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory.ModifyResponseGatewayFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.support.ipresolver.RemoteAddressResolver;
import org.springframework.cloud.gateway.support.ipresolver.XForwardedRemoteAddressResolver;
import org.springframework.context.annotation.Bean;
import org.tgit.gateway.common.ApiConstants;
import org.tgit.gateway.common.MD5Utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
/**
 * 
* @ClassName: CustomRouteLocatorConfig   
* @Description: (自定义路由配置)   
* @author fanzaijun 
* @date 2018年8月8日 下午2:38:57   
*
 */
//@Configuration
public class CustomRouteLocatorConfig implements InitializingBean {
	
	@Resource(name = "gatewayCache")
	Cache  gatewayCache;
	
	/**
	 * 
	* @Title: customRouteLocator   
	* @Description: (自定义动态路由)
	* @author fanzaijun  
	* @date 2018年8月8日 下午5:40:17   
	* @param builder
	* @return  
	* RouteLocator  
	* @throws (没有异常抛出)
	 */
	@Bean
	public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
		RemoteAddressResolver resolver = XForwardedRemoteAddressResolver
				.maxTrustedIndex(100);
		return builder.routes()
				//模糊匹配,目的是API未注册，直接返回
//				.route(r->r.path("/**").uri("forward:///rest/gateway/manager/error/101"))
				//页面路由访问页面-代理方式
				.route(r->r.path("/gateway").filters((f)->f.stripPrefix(1)).uri("http://101.132.176.148:9999/auth/login"))
				.route(r->r.path("/static/**").filters((f)->f.stripPrefix(1)).uri("http://101.132.176.148:9999"))
				//
				.route(r->r.path("/platform").uri("http://passport.bonree.com"))
				.route(r->r.path("/server").uri("http://server.bonree.com"))
				//http://cloud.spring.io/spring-cloud-static/Finchley.RC2/multi/multi__gatewayfilter_factories.html#_stripprefix_gatewayfilter_factory
				.route(r->r.path("/browser").uri("http://www.baidu.com"))
				.route(r->r.path("/sdk").uri("http://sdk.bonree.com"))
				.route(r->r.path("/sdk").uri("http://server.bonree.com"))
				.route(r->r.path("/net").uri("http://net.bonree.com"))
				.route(r->r.path("/registerApi/**").uri("http://localhost:9100/testApi"))
				.route("direct-route",
					    r -> r.remoteAddr("192.168.0.23").and().path("netdirect")
					        .uri("http://net.bonree.com"))
				.route("proxied-route",
					    r -> r.remoteAddr(resolver,  "192.168.0.23").and().path("netproxy")
					        .uri("http://net.bonree.com"))
				.route("/ServerWebExchange",
						r -> r.remoteAddr(resolver,  "192.168.0.23").and().path("netproxy")
						.uri("http://net.bonree.com"))
				.build();
	}
	/**
	 * 
	* @Title: preGatewayFilterFactory   
	* @Description: (前置过滤器)
	* @author fanzaijun  
	* @date 2018年8月8日 下午6:10:25   
	* @return  
	* PreGatewayFilterFactory  
	* @throws (没有异常抛出)
	 */
//	@Bean
//	public PreGatewayFilterFactory preGatewayFilterFactory() {
//		return new PreGatewayFilterFactory();
//	}
	/**
	 * 
	* @Title: postGatewayFilterFactory   
	* @Description: TODO(响应处理器)
	* @author fanzaijun  
	* @date 2018年8月8日 下午6:10:44   
	* @return  
	* PostGatewayFilterFactory  
	* @throws (没有异常抛出)
	 */
//	@Bean
//	public PostGatewayFilterFactory postGatewayFilterFactory() {
//		return new PostGatewayFilterFactory();
//	}

	@Override
	public void afterPropertiesSet() throws Exception {
		//路由表嵌入
		Set<Element> routerSet=new HashSet<Element>();
		Map<String,String> apiInfo=new HashMap<String,String>();
		Element ele=new Element(ApiConstants.GATEWAY_PRE_KEY + MD5Utils.md5("/browser"),apiInfo);
		//this.gatewayCache.put
	}
}
